package com.lab.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lab.dao.CollegeDao;
import com.lab.dao.CourseDao;
import com.lab.dao.MajorDao;
import com.lab.dao.PassWordDao;
import com.lab.dao.ScoreDao;
import com.lab.dao.StudentDao;
import com.lab.dao.impl.CollegeDaoImpl;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.dao.impl.PassWordDaoImpl;
import com.lab.dao.impl.ScoreDaoImpl;
import com.lab.dao.impl.StudentDaoImpl;
import com.lab.po.Student;

//控制层 -更新逻辑处理
@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {


	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = (String) request.getParameter("method");
		if ("updateStudent".equals(method)) {
			updateStudent(request, response);
		}else if ("updateCollege".equals(method)) {
			updateCollege(request, response);
		}else if("updateMajor".equals(method)){
			updateMajor(request, response);
		}else if("updateCourse".equals(method)){
			updateCourse(request, response);
		}else if("updateScore".equals(method)){
			updateScore(request, response);
		}else if("updateStuPwd".equals(method)){
			updateStuPwd(request, response);
		}else if("updateStatus".equals(method)){
			updateStatus(request, response);
		}
		
	}
	
	//更改选课状态
	private void updateStatus(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int status=Integer.parseInt(request.getParameter("status"));
		CourseDao cd=new CourseDaoImpl();
		cd.updateStatus(status);
		request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request,response);
	}

	//重置学生密码
	private void updateStuPwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int stId=Integer.parseInt(request.getParameter("stId"));
		PassWordDao pd=new PassWordDaoImpl();
		Map map=new HashMap();
		map.put("stId", stId);
		map.put("pwd", 123456);
		pd.updateStuPwd(map);
		String mainPage="common/success.jsp";
		request.setAttribute("mainPage",mainPage);
		System.out.println("重置成功!!");
		request.getRequestDispatcher("main.jsp").forward(request,response);
	}

	//录入分数信息之后实现页面跳转
	private void updateScore(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int stId=Integer.parseInt(request.getParameter("stId"));
		int count=Integer.parseInt(request.getParameter("count"));
		ScoreDao sd=new ScoreDaoImpl();
		for(int i=0;i<count;i++){
			double sc_score=Double.parseDouble(request.getParameter("sc_score"+i));
			int cou_id=Integer.parseInt(request.getParameter("cou_id"+i));
			Map map=new HashMap();
			map.put("stId", stId);
			map.put("cou_id", cou_id);
			map.put("sc_score", sc_score);
			sd.updateScore(map);
		}
		request.getRequestDispatcher("showAllServlet?method=showStudent").forward(request,response);
		
	}

	//更新课程信息之后实现页面跳转
	private void updateCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int cou_id=Integer.parseInt(request.getParameter("cou_id"));
		String cou_name = request.getParameter("cou_name");
		CourseDao cd=new CourseDaoImpl();
		Map map = new HashMap();
		map.put("cou_id", cou_id);
		map.put("cou_name", cou_name);
		cd.updateCourse(map);
		request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request,response);
		
	}

	//更新专业信息之后实现页面跳转
	private void updateMajor(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int col_id=Integer.parseInt(request.getParameter("college"));
		int maj_id=Integer.parseInt(request.getParameter("maj_id"));
		String maj_name=request.getParameter("maj_name");
		MajorDao majordao = new MajorDaoImpl();
		Map map = new HashMap();
		map.put("col_id", col_id);
		map.put("maj_id", maj_id);
		map.put("maj_name", maj_name);
		majordao.updateMajor(map);
		request.getRequestDispatcher("showAllServlet?method=showColMaj").forward(request,response);

	}
	//更新学院信息之后实现页面跳转
	private void updateCollege(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int col_id=Integer.parseInt(request.getParameter("col_id"));
		String col_name = request.getParameter("col_name");
		CollegeDao collegedao=new CollegeDaoImpl();
		Map map = new HashMap();
		map.put("col_id", col_id);
		map.put("col_name", col_name);
		collegedao.updateCollege(map);
		request.getRequestDispatcher("showAllServlet?method=showColMaj").forward(request,response);
	}
	//更新学生信息之后实现页面跳转
	private void updateStudent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int stId =Integer.parseInt(request.getParameter("stId"));
		String stName = request.getParameter("stName");
		String stSex = request.getParameter("stSex");
		String stAge = request.getParameter("stAge");
		String stTel = request.getParameter("stTel");
		int maj_id=Integer.parseInt(request.getParameter("major"));
		System.out.println(maj_id);
		String stAddress = request.getParameter("stAddress");
		Student stu = new Student(stId, stName, stSex, stAge, stTel, maj_id,
				stAddress);
		StudentDao studao = new StudentDaoImpl();

		boolean flag = studao.updateStudent(stu);
		if (flag) {
			request.getRequestDispatcher("showAllServlet?method=showStudent").forward(request,
					response);
		} else {
			request.getRequestDispatcher("main.jsp").forward(request,
					response);
		}
		
	}

}
